package com.anhry.www.safety.manage.oa.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.www.safety.manage.oa.bean.TbOaCallMsgView;
import com.anhry.www.safety.manage.oa.service.CallService;
import com.anhry.www.safety.util.Loggers;
import com.anhry.www.safety.util.dao.UtilDao;

/**
 * 
 * @author chen
 * @date 2012-9-10
 */
@Service
public class CallServiceImpl implements CallService {

	private static Logger logger = Logger.getLogger(CallServiceImpl.class);

	@Resource
	private UtilDao utilDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.anhry.www.safety.manage.oa.service.impl.CallService#getUtilDao()
	 */
	public UtilDao getUtilDao() {
		return utilDao;
	}

	public List<TbOaCallMsgView> loadCallMsgList(String keyWord, String beginTime, String endTime, TbCorpInfo corp, String isRead, final int startRow, final int size) throws Exception {
		if (null == keyWord) {
			keyWord = "";
		}
		StringBuffer sbf = new StringBuffer("from TbOaCallMsgView msg where msg.msgContent like '%" + keyWord + "%' and msg.isDele=0 and msg.tbCorpInfo.id='" + corp.getId() + "' and msg.userTypes=1");
		if (size == 0) {
			sbf = new StringBuffer("select count(*) from TbOaCallMsgView msg where msg.msgContent like '%" + keyWord + "%' and msg.isDele=0 and msg.tbCorpInfo.id='" + corp.getId() + "' and msg.userTypes=1");
		}
		if (null != beginTime && !"".equals(beginTime) && null != endTime && !"".equals(endTime)) {
			sbf.append(" and to_char(msg.msgTime,'yyyy-mm-dd') between '" + beginTime + "' and '" + endTime + "'");
		}
		if (null != isRead && !"".equals(isRead) && !"3".equals(isRead)) {
			sbf.append(" and msg.isRead='" + isRead + "'");
		}
		sbf.append(" and (msg.callType=0 or msg.callType=2) order by msg.msgTime desc");// 
		final String sql = sbf.toString();
		Session session = this.getUtilDao().getHibernateSession();
		List list = new ArrayList();
		try {
			Query query = null;
			query = session.createQuery(sql);
			if (size > 0) {
				query.setFirstResult(startRow);
				query.setMaxResults(size);
			}
			list = query.list();
		} catch (Exception t) {
			t.printStackTrace();
			Loggers.error("OADaoImpl:List<TbOaCallMsg> loadCallMsgList(String keyWord,String beginTime, String endTime, TbUser user, final int startRow,final int size)" + t.getMessage());
			try {
				throw t;
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.anhry.www.safety.manage.oa.service.impl.CallService#getNotReadCallSize(com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo)
	 */
	public int getNotReadCallSize(TbCorpInfo corp) throws Exception {
		StringBuffer sbf = new StringBuffer();
		sbf = new StringBuffer("select count(*) from TbOaCallMsg msg where msg.isDele=0 and msg.tbCorpInfo.id='" + corp.getId() + "' and msg.isRead=0");
		final String sql = sbf.toString();
		Session s = utilDao.getHibernateSession();
		List list = new ArrayList();
		try {
			Query query = null;
			query = s.createQuery(sql);
			list = query.list();
		} finally {
			if (s != null && s.isOpen()) {
				s.close();
			}
		}
		return Integer.parseInt(list.get(0).toString());
	}

}
